<?php
namespace app\cms\controller\v1;

use app\cms\validate\Warehouse as WarehouseValidate;
use app\common\controller\BaseController;
use app\common\exception\Client;
use app\common\exception\Server;
use app\common\service\Warehouse as WarehouseService;
use http\Client\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;
use think\response\Json;

class Warehouse extends BaseController
{
    /**
     * 创建货架(每次创建一个货架，就创建多层)
     * @param Request $request
     * @return Json
     * @throws Client
     */
    public function createShelf(Request $request): Json
    {
        // 1. 参数接收
        $params = $request->only(['shelfno', 'total_layers', 'area']);
        // 2. 参数验证
        $validate = new WarehouseValidate();
        if (!$validate->scene('create')->check($params)) {
            throw new Client(211001,$validate->getError());
        }
        // 2. 调用服务层
        $result = WarehouseService::createShelf($params);
        return writeJson(200, $result, '货架创建成功');
    }

    /**
     * 创建一个货架层
     * @param WarehouseService $warehouseService
     * @return \think\response\Json
     * @throws Client
     */
    public function createShelye(Request $request): Json{
        // 接收参数
        $params = $request->only([
            'area', 'shelfno', 'layerno', 'layertype', 'is_locked'
        ], 'post');
        // 验证参数
        $validate = new WarehouseValidate();
        if(!$validate->scene('createone')->check($params)){
            throw new Client(211001,$validate->getError());
        }
        // 调用服务层
        return WarehouseService::createWarehouseLayer($params);
    }

    /**
     * 删除货架层记录
     * @param Request $request
     * @return Json
     * @throws Client
     * @throws DbException
     */
    public function delete(Request $request): Json
    {
        // 验证参数
        $params = $request->only(['ids']);
        $validate = new WarehouseValidate();
        if(!$validate->scene('batchDelete')->check($params)){
            throw new Client(211001,$validate->getError());
        }
        // 调用服务
        $result = (new WarehouseService())->batchDelete($params['ids']);
        return writeJson(200, $result, '删除成功');
    }

    /**
     * 更新货架层记录
     * @param Request $request
     * @return Json
     * @throws Client
     * @throws DbException
     * @throws Server
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function update(Request $request): Json
    {
        // 验证参数
        $params = $request->param();
        $validate = new WarehouseValidate();
        if(!$validate->scene('update')->check($params)){
            throw new Client(211001,$validate->getError());
        }
        //调用服务层处理
        $result = (new WarehouseService())->update($params);
        //返回参数
        return writeJson(200, $result, '更新成功');
    }

    /**
     * 切换货架状态
     * @param Request $request
     * @return Json
     * @throws Client
     */
    public function switchStatus(Request $request): Json
    {
        // 1. 参数接收
        $params = $request->only(['id', 'is_locked']);
        // 2. 参数验证
        $validate = new WarehouseValidate();
        if (!$validate->scene('switch')->check($params)) {
            throw new Client(211001,$validate->getError());
        }

        WarehouseService::switchLayerStatus($params['id'], $params['is_locked']);
        return writeJson(
            200,
            $params,
            $params['is_locked'] ? '已禁用' : '已开启'
        );

    }


    /**
     * 分页查询货架层数据
     * @param Request $request
     * @return Json
     */
    public function list(Request $request): Json
    {
        // 获取所有查询参数
        $params = $request->get();
        $paginator = (new WarehouseService())->getPaginatedList($params);
        return $this->apiPaginate($paginator);
    }


}